/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pushy.client;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import pushy.server.Objekt;

/**
 *
 * @author philipp
 */
public class Levelansicht extends javax.swing.JPanel implements KeyListener {

    Client client;
    pushy.server.Level level = new pushy.server.Level("", "");
    Graphics2D comp2d;
    Darstellung darstellung = new Darstellung();
    
    /**
     * Creates new form Levelansicht
     * @param client
     */
    public Levelansicht(final Client client) {
        initComponents();
        client.getGUI().setTitle("Pushynator - Spiel");
        
        this.client = client;
        setSize(1512, 970);
        setPreferredSize(new Dimension(1512, 970));
        
        input.requestFocusInWindow();
        
        input.addKeyListener(new java.awt.event.KeyAdapter() {
        @Override
        public void keyReleased(java.awt.event.KeyEvent evt) {
            String s = input.getText();
            if(s.equals("a")) {
                client.movement("Left");
            }else if(s.equals("w")) {
                client.movement("Up");
            }else if(s.equals("d")) {
                client.movement("Right");
            }else if(s.equals("s")) {
                client.movement("Down");
            }
            input.setText("");
            input.grabFocus();
        }});
    }

    @Override
    public void paintComponent(Graphics comp) {
        super.paintComponent(comp);
        Graphics2D comp2d = (Graphics2D) comp;
        this.comp2d = comp2d;
        comp2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        comp2d.setClip(0, 0, 1500, 950);
        
        paintLevel();
    }
    
    public boolean paintLevel() {
        if(comp2d == null) {
            client.getGUI().getContentPane().repaint();
            return false;
        }
        for(int x = 0; x < level.getX(); x++) {
            for(int y = 0; y < level.getY(); y++) {
                Objekt o = level.getWichtigesObjekt(x, y);
                if(o == null) {
                    comp2d.drawImage(((Image) darstellung.anzeigen(0)), x*75, y*75+35,this);
                    System.err.println("Objekt an " + x + " " + y + " ist null");
                }else {
                    comp2d.drawImage(((Image) darstellung.anzeigen(level.getWichtigesObjekt(x, y).getId())), x*75, y*75+35,this);
                }
            }
        }
        return true;
    }
    
    
    
    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
    }

    @Override
    public void keyReleased(KeyEvent e) {     
        int keyCode = e.getKeyCode();
        
        if (keyCode == KeyEvent.VK_LEFT){
            client.movement("Left");
        }
        else if (keyCode == KeyEvent.VK_RIGHT){
            client.movement("Right");
        }
        else if (keyCode == KeyEvent.VK_UP){
            client.movement("Up");
        }
        else if (keyCode == KeyEvent.VK_DOWN) {
            client.movement("Down");
        }
        System.out.println("Taste: " + keyCode);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        input = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        input.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inputActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Microsoft Tai Le", 1, 18)); // NOI18N
        jLabel1.setText("<-- In dem Feld mit w, a, s und d bewegen");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(input, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(input, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(261, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void inputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputActionPerformed

    }//GEN-LAST:event_inputActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField input;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables

}
